<template>
    <el-card>
        <div class="arrange-panel-header">
            <div class="class-profiles">
                <div class="title">{{heardTitle}}</div>
            </div>
            <el-button class="float-right" @click="back">{{$t('common.btn.back')}}</el-button>
        </div>
        <div style="margin-top: 24px;" id="course-form">
            <el-form
                :model="courseForm"
                label-position="right"
                label-width="120px"
                :rules="pathKey=='edit'&&courseForm.enabled==0?null:courseRules"
                ref="courseForm"
                label-suffix=":"
            >
                <div>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.college')" prop="college">
                                <el-select
                                    :disabled="getReadOnly('college')"
                                    v-model="courseForm.college"
                                    @change="changeCollege"
                                    style="width:100%;"
                                    filterable
                                >
                                    <el-option
                                        v-for="item in options.college"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('label.trainingLevel')" prop="trainingLevel">
                                <el-select
                                    :disabled="getReadOnly('trainingLevel')"
                                    v-model="courseForm.trainingLevel"
                                    @change="reSetCode"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.trainingLevel"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.courseName')" prop="name">
                                <el-input
                                    :disabled="getReadOnly('name')"
                                    v-model="courseForm.name"
                                    :placeholder="$t('common.pla.pleaseInput')"
                                    type="text"
                                    maxlength="100"
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="8" v-if="showCourseCode">
                            <el-form-item
                                :label="$t('course.courseCode')"
                                v-if="pathKey=='edit'||auto"
                            >
                                <el-input
                                    v-model="courseForm.code"
                                    :disabled="true"
                                    type="text"
                                    :placeholder="$t('common.pla.pleaseInput')"
                                    maxlength="100"
                                />
                            </el-form-item>
                            <el-form-item :label="$t('course.courseCode')" prop="code" v-else>
                                <el-input
                                    v-model="courseForm.code"
                                    :disabled="readOnly"
                                    type="text"
                                    maxlength="32"
                                    style="width: 63%;"
                                    ref="courseFormCode"
                                    :placeholder="$t('common.pla.pleaseInput')"
                                />
                                <el-button
                                    id="auto-btn"
                                    @click="autoCode"
                                    type="text"
                                    style="width: 35%;font-size: 14px;"
                                >{{$t("course.autoAllocation")}}</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.courseNameEn')" prop="nameEn">
                                <el-input
                                    :disabled="getReadOnly('nameEn')"
                                    v-model="courseForm.nameEn"
                                    type="text"
                                    :placeholder="$t('common.pla.pleaseInput')"
                                    maxlength="300"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.score')" prop="credits">
                                <el-input
                                    :disabled="getReadOnly('credits')"
                                    v-model="courseForm.credits"
                                    type="text"
                                    maxlength="10"
                                    @input="calPeriod"
                                    :placeholder="$t('common.pla.pleaseInput')"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.term')" prop="term">
                                <el-select
                                    :disabled="getReadOnly('term')"
                                    v-model="courseForm.term"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.term"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :span="8">
                            <el-form-item :label="$t('courses.classHour')" prop="period">
                                <el-input
                                    :disabled="getReadOnly('period')"
                                    v-model="courseForm.period"
                                    :placeholder="$t('common.pla.pleaseInput')"
                                    type="text"
                                    maxlength="10"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.scoreType')" prop="scoreType">
                                <el-select
                                    :disabled="getReadOnly('scoreType')"
                                    v-model="courseForm.scoreType"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.scoreType"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.courseProperty')" prop="nature">
                                <el-select
                                    :disabled="getReadOnly('nature')"
                                    v-model="courseForm.nature"
                                    style="width: 100%;"
                                    @change="changeNature"
                                >
                                    <el-option
                                        v-for="item in options.nature"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.courseCategory')" prop="label">
                                <el-select
                                    :disabled="getReadOnly('label')"
                                    v-model="courseForm.label"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.tableLabel"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.formLearning')" prop="formLearning">
                                <el-select
                                    :disabled="getReadOnly('formLearning')"
                                    v-model="courseForm.formLearning"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.formLearning"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                :label="$t('course.teachingLanguage')"
                                prop="teachingLanguage"
                            >
                                <el-select
                                    :disabled="getReadOnly('teachingLanguage')"
                                    v-model="courseForm.teachingLanguage"
                                    style="width: 100%"
                                >
                                    <el-option
                                        v-for="item in options.teachingLanguage"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.teachMode')" prop="teachModeList">
                                <div v-if="getReadOnly('teachModeList')">
                                    <el-input :disabled="true" v-model="courseForm.teachModeI18n" />
                                </div>
                                <div v-else>
                                    <el-select
                                        v-model="courseForm.teachModeList"
                                        multiple
                                        collapse-tags
                                        style="width: 100%;"
                                    >
                                        <el-option
                                            v-for="item in options.teachModeList"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        ></el-option>
                                    </el-select>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.precourse')">
                                <el-tooltip
                                    class="item"
                                    effect="light"
                                    :content="courseForm.precourse"
                                    placement="top-start"
                                    :disabled="getDisabledVal(getReadOnly('precourse'),courseForm.precourse)"
                                >
                                    <el-input
                                        :disabled="getReadOnly('precourse')"
                                        v-model="courseForm.precourse"
                                        :placeholder="$t('common.pla.pleaseInput')"
                                        type="text"
                                        maxlength="200"
                                    />
                                </el-tooltip>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="Prerequisite">
                                <el-tooltip
                                    class="item"
                                    effect="light"
                                    :content="courseForm.precourseEn"
                                    placement="top-start"
                                    :disabled="getDisabledVal(getReadOnly('precourseEn'),courseForm.precourseEn)"
                                >
                                    <el-input
                                        :disabled="getReadOnly('precourseEn')"
                                        v-model="courseForm.precourseEn"
                                        :placeholder="$t('common.pla.pleaseInput')"
                                        type="text"
                                        maxlength="200"
                                    />
                                </el-tooltip>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.enabled')" prop="enabled">
                                <el-select
                                    :disabled="getReadOnly('enabled')"
                                    v-model="courseForm.enabled"
                                    style="width: 100%;"
                                    @change="handleEnabled"
                                >
                                    <el-option
                                        v-for="item in options.enabled"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                :label="$t('course.assessmentMode')"
                                prop="assessmentMode"
                            >
                                <el-select
                                    :disabled="getReadOnly('assessmentMode')"
                                    v-model="courseForm.assessmentMode"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.assessmentMode"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('course.headTeacherList')" prop="headTeacher">
                                <div v-if="getReadOnly('headTeacher')&&!courseForm.headTeacher">
                                    <el-input disabled></el-input>
                                </div>
                                <div v-else>
                                    <TeacherSelect
                                        v-model="courseForm.headTeacher"
                                        :condition="headTeacherCondition"
                                        :showAllTeacher="true"
                                        value="code"
                                        :disabled="getReadOnly('headTeacher')"
                                        placeholder="请输入"
                                    ></TeacherSelect>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8" v-if="showCrossTerm">
                            <el-form-item :label="$t('course.crossTerm')" prop="crossTerm">
                                <el-select
                                    :disabled="getReadOnly('crossTerm')"
                                    v-model="courseForm.crossTerm"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.crossTerm"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8" id="for-major">
                            <el-form-item :label="$t('适用专业')" prop="majorText">
                                <el-tooltip
                                    class="item"
                                    effect="light"
                                    :content="courseForm.majorTextI18n"
                                    placement="top-start"
                                    :disabled="getDisabledVal(getReadOnly('majorList'),courseForm.majorTextI18n)"
                                >
                                    <el-select
                                        :disabled="getReadOnly('majorList')"
                                        v-model="courseForm.majorList"
                                        style="width:100%;height:33px"
                                        multiple="true"
                                        collapse-tags
                                        filterable="true"
                                    >
                                        <el-option
                                            v-for="item in options.majorList"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        ></el-option>
                                    </el-select>
                                </el-tooltip>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('是否跨院系课程')" prop="crossCollege">
                                <el-select
                                    :disabled="getReadOnly('crossCollege')"
                                    v-model="courseForm.crossCollege"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.crossTerm"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8" v-if="committee">
                            <el-form-item
                                :label="$t('teachers.subjectMajorCommittee')"
                                prop="subjectMajorCode"
                            >
                                <el-select
                                    v-model="courseForm.subjectMajorCode"
                                    style="width: 100%;"
                                    :disabled="readOnly"
                                    @change="changeSubMajor"
                                >
                                    <el-option
                                        v-for="item in options.subjectMajorCode"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8" v-if="committee">
                            <el-form-item
                                :label="$t('teachers.appointApprover')"
                                prop="appointUserCode"
                            >
                                <el-select
                                    v-model="courseForm.appointUserCode"
                                    :disabled="readOnly"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item in options.appointUserCode"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <el-row>
                    <el-col :span="24">
                        <el-form-item :label="$t('course.teachersList')">
                            <div v-if="getReadOnly('teachersList')">
                                <el-input :disabled="true" v-model="teacherLabels" />
                            </div>
                            <div v-else>
                                <TeacherSelect
                                    v-model="courseForm.teachersList"
                                    :condition="teacherCondition"
                                    :showAllTeacher="true"
                                    value="code"
                                    multiple="true"
                                    style="width: 80%;"
                                    collapse-tags
                                    :placeholder="'请输入'"
                                ></TeacherSelect>
                            </div>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item
                            :label="$t('course.introductionCourse')"
                            prop="introductionCourse"
                        >
                            <el-input
                                :disabled="getReadOnly('introductionCourse')"
                                v-model="courseForm.introductionCourse"
                                type="textarea"
                                rows="3"
                                style="width: 80%;"
                                :placeholder="$t('common.pla.pleaseInput')"
                                maxlength="1000"
                            />
                            <span
                                v-if="editTip"
                                style="color:#909399;"
                            >&nbsp;&nbsp;&nbsp;&nbsp;{{$t('courses.wordsLimited2')}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24" style="margin-top: 40px;">
                        <el-form-item
                            :label="$t('course.engIntroductionCourse')"
                            prop="engIntroductionCourse"
                        >
                            <el-input
                                :disabled="getReadOnly('engIntroductionCourse')"
                                v-model="courseForm.engIntroductionCourse"
                                type="textarea"
                                rows="3"
                                style="width: 80%;"
                                :placeholder="$t('common.pla.pleaseInput')"
                                maxlength="2000"
                            />
                            <span
                                v-if="editTip"
                                style="color:#909399;"
                            >&nbsp;&nbsp;&nbsp;&nbsp;{{$t('courses.wordsLimited')}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row style="margin-top: 40px;">
                    <el-col :span="24">
                        <el-form-item :label="$t('course.schedule')">
                            <el-input
                                :disabled="getReadOnly('schedule')"
                                v-model="courseForm.schedule"
                                :placeholder="$t('common.pla.pleaseInput')"
                                type="textarea"
                                rows="3"
                                style="width: 80%;"
                                maxlength="2000"
                            />
                            <span
                                v-if="editTip"
                                style="color:#909399;"
                            >&nbsp;&nbsp;&nbsp;&nbsp;{{$t('courses.wordsLimited')}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row style="margin-top: 40px;">
                    <el-col :span="24">
                        <el-form-item :label="$t('course.scheduleEn')">
                            <el-input
                                :disabled="getReadOnly('scheduleEn')"
                                v-model="courseForm.scheduleEn"
                                :placeholder="$t('common.pla.pleaseInput')"
                                type="textarea"
                                rows="3"
                                style="width: 80%;"
                                maxlength="2000"
                            />
                            <span
                                v-if="editTip"
                                style="color:#909399;"
                            >&nbsp;&nbsp;&nbsp;&nbsp;{{$t('courses.wordsLimited')}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row style="margin-top: 40px;">
                    <el-col :span="24">
                        <el-form-item :label="$t('course.teacherTermInfo')">
                            <el-input
                                :disabled="getReadOnly('teacherTermInfo')"
                                v-model="courseForm.teacherTermInfo"
                                :placeholder="$t('course.inputTeacherInfo')"
                                type="textarea"
                                rows="3"
                                style="width: 80%;"
                                maxlength="2000"
                            />
                            <span
                                v-if="editTip"
                                style="color:#909399;"
                            >&nbsp;&nbsp;&nbsp;&nbsp;{{$t('courses.wordsLimited')}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row style="margin-top: 40px;">
                    <el-col :span="24">
                        <el-form-item :label="$t('message.remark')">
                            <el-input
                                :disabled="getReadOnly('remark')"
                                type="textarea"
                                rows="3"
                                :placeholder="$t('common.pla.pleaseInput')"
                                v-model="courseForm.remark"
                                style="width: 80%;"
                                maxlength="2000"
                            />
                            <span
                                v-if="editTip"
                                style="color:#909399;"
                            >&nbsp;&nbsp;&nbsp;&nbsp;{{$t('courses.wordsLimited')}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row style="margin-top: 40px;" v-if="!readOnly">
                    <el-col :span="24" class="text-right">
                        <el-button @click="back">{{$t("message.cancel")}}</el-button>
                        <el-button type="primary" @click="saveCourse">{{$t("message.save")}}</el-button>
                    </el-col>
                </el-row>
            </el-form>
        </div>
    </el-card>
</template>
<script>
import { autoCode } from 'common/src/api/courseTemplate'
import {
    addCourse,
    editCourse,
    queryCourseTimeSetting,
    getSubjectMajorCommitteeByDept,
    getSubjectMajorRoleByCommittee
} from 'common/src/api/resource/course'
import { findListByFaculty, findTeacherList } from 'common/src/api/resource/teachers'
import { getCoursePermission } from 'common/src/api/resource/course'
import mixin from 'common/src/mixins/index';
export default {
    name: 'CourseOperation',
    mixins: [mixin],
    props: {
        // 从父组件获取数据字典
        options: {
            type: Object
        },
        courseForm: {
            type: Object
        },
        pathKey: {
            type: String
        },
        heardTitle: {
            type: String
        },
        managerDeptId: {},
        auto: {
            type: Boolean,
            default: true
        },
        showCourseCode: {
            type: Boolean,
            default: true
        },
        showCrossTerm: {
            type: Boolean,
            default: true
        },
        committee: {
            type: Boolean,
            default: false
        },
        isMaintain: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            editTip: true,
            courseRules: {
                code: [
                    {
                        required: true,
                        message: this.$t('course.inputCourseCode'),
                        trigger: 'blur'
                    }
                ],
                name: [
                    {
                        required: true,
                        message: this.$t('course.inputCourseName'),
                        trigger: 'blur'
                    },
                    {
                        validator: this.notBlank,
                        trigger: 'blur'
                    }
                ],
                nameEn: [
                    {
                        required: true,
                        message: this.$t('course.inputCourseName'),
                        trigger: 'blur'
                    },
                    {
                        validator: this.notBlank,
                        trigger: 'blur'
                    }
                ],
                college: [
                    {
                        required: true,
                        message: this.$t('course.selectCollege'),
                        trigger: 'blur'
                    }
                ],
                formLearning: [
                    {
                        required: true,
                        message: this.$t('course.selectCourseType'),
                        trigger: 'blur'
                    }
                ],
                trainingLevel: [
                    {
                        required: true,
                        message: this.$t('course.selectTrainingLevel'),
                        trigger: 'blur'
                    }
                ],
                credits: [
                    {
                        required: true,
                        message: this.$t('course.inputCourseScore'),
                        trigger: 'blur'
                    },
                    {
                        validator: this.doubleReg,
                        trigger: 'blur'
                    },
                    {
                        validator: this.notBlank,
                        trigger: 'blur'
                    }
                ],
                period: [
                    {
                        required: true,
                        message: this.$t('course.inputStudyTime'),
                        trigger: 'blur'
                    },
                    {
                        validator: this.doubleReg,
                        trigger: 'blur'
                    },
                    {
                        validator: this.notBlank,
                        trigger: 'blur'
                    }
                ],
                introductionCourse: [
                    {
                        required: true,
                        message: this.$t('course.inputCourseInfo'),
                        trigger: 'blur'
                    },
                    {
                        validator: (rule, value, callback) => {
                            if (
                                this.courseForm.introductionCourse.length >= 100 &&
                                this.courseForm.introductionCourse.length <= 1000
                            ) {
                                callback()
                            } else {
                                callback(
                                    new Error(
                                        '长度必须在100~1000，当前长度:' +
                                            this.courseForm.introductionCourse.length
                                    )
                                )
                            }
                        },
                        trigger: 'blur'
                    }
                ],
                engIntroductionCourse: [
                    {
                        required: true,
                        message: this.$t('course.inputCourseInfoEn'),
                        trigger: 'blur'
                    },
                    {
                        validator: (rule, value, callback) => {
                            if (
                                this.courseForm.engIntroductionCourse.length >= 100 &&
                                this.courseForm.engIntroductionCourse.length <= 2000
                            ) {
                                callback()
                            } else {
                                callback(
                                    new Error(
                                        '长度必须在100~2000，当前长度:' +
                                            this.courseForm.engIntroductionCourse.length
                                    )
                                )
                            }
                        },
                        trigger: 'blur'
                    }
                ],
                label: [
                    {
                        required: true,
                        message: this.$t('course.selectCourseCategory'),
                        trigger: 'blur'
                    }
                ],
                term: [
                    {
                        required: true,
                        message: this.$t('course.selectSemester'),
                        trigger: 'blur'
                    }
                ],
                nature: [
                    {
                        required: true,
                        message: this.$t('course.selectCourseNature'),
                        trigger: 'blur'
                    }
                ],
                scoreType: [
                    {
                        required: true,
                        message: this.$t('course.selectScoreType'),
                        trigger: 'blur'
                    }
                ],
                teachingLanguage: [
                    {
                        required: true,
                        message: this.$t('course.selectTeachingLanguage'),
                        trigger: 'blur'
                    }
                ],
                precourse: [
                    {
                        required: true,
                        message: this.$t('course.inputPreparatoryCourse'),
                        trigger: 'blur'
                    }
                ],
                precourseEn: [
                    {
                        required: true,
                        message: this.$t('course.inputPreparatoryCourse'),
                        trigger: 'blur'
                    }
                ],
                teachModeList: [
                    {
                        required: true,
                        message: this.$t('course.selectTeachingMode'),
                        trigger: 'blur'
                    }
                ],
                headTeacher: [
                    {
                        validator: (rule, value, callback) => {
                            if(this.pathKey === 'edit'&&this.courseForm.enabled==0){
                                 return callback();                                 
                            }else if(this.courseForm.headTeacher==null||this.courseForm.headTeacher==''){
                                 return callback(new Error(this.$t('course.selectCourseLeader')));
                            }else{
                                callback();
                            }
                        },
                        trigger: 'blur'
                    }
                ],
                teacherTermInfo: [
                    {
                        required: true,
                        message: this.$t('course.inputTeacherInfo'),
                        trigger: 'blur'
                    }
                ],
                enabled: [
                    {
                        required: true,
                        message: this.$t('course.selectUseCourse'),
                        trigger: 'blur'
                    }
                ],
                crossTerm: [
                    {
                        required: true,
                        message: this.$t('course.selectCrossTerm'),
                        trigger: 'blur'
                    }
                ],
                requirement: [
                    {
                        required: true,
                        message: this.$t('course.inputTeachingTarget'),
                        trigger: 'blur'
                    }
                ],
                requirementEn: [
                    {
                        required: true,
                        message: this.$t('course.inputTeachingTargetEn'),
                        trigger: 'blur'
                    }
                ],
                assessmentMode: [
                    {
                        required: true,
                        message: this.$t('course.selectCheckMode'),
                        trigger: 'blur'
                    }
                ],
                crossCollege: [
                    {
                        required: true,
                        message: this.$t('请选择是否跨院系课程'),
                        trigger: 'blur'
                    }
                ],
                subjectMajorCode: [
                    {
                        required: true,
                        message: this.$t('请选择学科专业委员会'),
                        trigger: 'blur'
                    }
                ],
                appointUserCode: [
                    {
                        required: true,
                        message: this.$t('请选择指定审核人'),
                        trigger: 'blur'
                    }
                ]
            },
            readOnly: false,
            headTeacherCondition: null,
            teacherCondition: null,
            teacherLabels: null,
            enabledPermission: false,
            readCodes: []
        }
    },
    created() {
        this.init()
    },
    methods: {
        getReadOnly: function(code) {
            if (this.enabledPermission) {
                return this.readCodes.includes(code)
            }
            return this.readOnly
        },
        getDisabledVal: function(readOnly, val) {
            return readOnly && val != null && val != '' ? false : true
        },
        init() {
            this.editTip = true
            if (this.pathKey === 'check') {
                this.readOnly = true
                this.editTip = false
                this.headTeacherCondition = {
                    codeList: [this.courseForm.headTeacher]
                }
                this.teacherCondition = {
                    codeList: this.courseForm.teachersList
                }
            } else if (this.pathKey === 'add') {
                this.headTeacherCondition = {}
                this.teacherCondition = {}
            } else {
                //在课程维护，角色为教务员时，开启权限校验
                if (this.isMaintain && this.global.isAcdemicDean && !this.global.isAdmin) {
                    this.readOnly = true
                    queryCourseTimeSetting({
                        condition: {
                            college: this.courseForm.college,
                            page: false,
                            permissionsFlag: true
                        }
                    }).then(res => {
                        if (res.code == 200 && res.data.list.length > 0) {
                            this.enabledPermission = true
                            getCoursePermission().then(res => {
                                if (res.code == 200) {
                                    this.readCodes = res.data
                                        .filter(column => column.readOnly)
                                        .map(column => column.code)
                                    if (this.readCodes.length == res.data.length) {
                                        this.readOnly = true
                                    } else {
                                        this.readOnly = false
                                    }
                                }
                            })
                        }
                    })
                }
                this.headTeacherCondition = {
                    codeList: [this.courseForm.headTeacher]
                }
                this.teacherCondition = {
                    codeList: this.courseForm.teachersList
                }
            }
            //其他讲课人,只读时转换文本显示
            if (
                this.readOnly &&
                this.courseForm.teachersList &&
                this.courseForm.teachersList.length > 0
            ) {
                findTeacherList({ codeList: this.courseForm.teachersList }).then(res => {
                    if (res.code == 200 && res.data) {
                        this.teacherLabels = res.data.list
                            .map(t => t.name + '(' + t.code + ')')
                            .join('，')
                    }
                })
            }
        },
        doubleReg(rule, value, callback) {
            let number = new Number(value)
            if (isNaN(number)) {
                callback(new Error(this.$t('course.mustInputNumber')))
            } else {
                callback()
            }
        },
        notBlank(rule, value, callback) {
            if (value && value.toString().replace(/^\s+|\s+$/g, '') == '') {
                callback(new Error(this.$t('course.mustNotInputBlank')))
            } else {
                callback()
            }
        },
        calPeriod(val) {
            this.courseForm.period = (val * 18).toString()
        },
        changeCollege(val) {
            this.courseForm.code = ''
            //存在学科专业委员会,重置学科专业委员会和指定审核人
            if (this.committee) {
                this.courseForm.subjectMajorCode = null
                this.options.subjectMajorCode =[]           
                
                if(val){
                    getSubjectMajorCommitteeByDept(val).then(res=>{
                        if(res.code==200&&res.data){
                            this.options.subjectMajorCode = res.data.map(m=>{
                                return {label:m.subjectMajorName,value:m.subjectMajorCode}
                            })
                        }
                    })
                }
                this.courseForm.appointUserCode = null
                this.options.appointUserCode = []                
            }
        },
        reSetCode(val) {
            //  this.underGraduateCourseForm.code = "";
            this.courseForm.code = ''
        },

        changeNature(val) {
            this.options.tableLabel = []
            this.courseForm.label = ''
            let obj = []
            let labels = this.options.label
            let scoreTypeList = this.options.scoreType
            labels.forEach(item => {
                if (val == '1') {
                    if (
                        item.value == '01' ||
                        item.value == '02' ||
                        item.value == '03' ||
                        item.value == '07'
                    ) {
                        obj.push({ label: item.label, value: item.value })
                    }
                } else if (val == '2') {
                    if (
                        item.value == '04' ||
                        item.value == '05' ||
                        item.value == '06' ||
                        item.value == '07'
                    ) {
                        obj.push({ label: item.label, value: item.value })
                    }
                } else {
                    if (item.value == '08' || item.value == '09') {
                        obj.push({ label: item.label, value: item.value })
                    }
                }
            })
            this.options.tableLabel = obj
            // scoreTypeList.forEach(item => {
            //     if (val == "1" || val == "2") {
            //         this.courseForm.scoreType = "1";
            //     } else {
            //         this.courseForm.scoreType = "";
            //     }
            // });
        },
        changeSubMajor(val) {
            this.courseForm.appointUserCode = ''
            this.options.appointUserCode = []
            if (val) {
                getSubjectMajorRoleByCommittee(val).then(res => {
                    if (res.code == 200 && res.data.length > 0) {
                        for (const item of res.data) {
                            this.options.appointUserCode.push({
                                label: item.userName + '(' + item.userCode + ')',
                                value: item.userCode
                            })
                        }
                    }
                })
            }
        },
        //保存课程
        saveCourse() {
            this.$refs.courseForm.validate(valid => {
                if (!valid) {
                    this.$message({
                        type: 'error',
                        message: this.$t('course.courseInfoWriteError')
                    })
                    return
                }
                let obj = _.cloneDeep(this.courseForm)
                if (obj.establishOn) {
                    obj.establishOn = Number(new Date(obj.establishOn))
                }
                obj.managerDeptId = this.managerDeptId
                //处理专业
                //[]
                obj.majorText = obj.majorList.join(',')
                if (this.pathKey === 'edit') {
                    editCourse(obj).then(response => {
                        if (response.code == 200) {
                            this.showDialog = false
                            this.$message({
                                type: 'success',
                                message: this.$t('message.saveSuccess')
                            })
                            this.back()
                        }
                    })
                } else {
                    addCourse(obj).then(response => {
                        if (response.code == 200) {
                            this.showDialog = false
                            this.$message({
                                type: 'success',
                                message: this.$t('message.saveSuccess')
                            })
                            this.back()
                        }
                    })
                }
            })
        },

        autoCode() {
            let trainingLevel = ''
            let college = ''
            /* if (this.managerDeptId == 1) {
				trainingLevel = this.underGraduateCourseForm.trainingLevel;
				college = this.underGraduateCourseForm.college;
			} else {
				trainingLevel = this.courseForm.trainingLevel;
				college = this.courseForm.college;
			} */
            if (this.managerDeptId != 1) {
                trainingLevel = this.courseForm.trainingLevel
                college = this.courseForm.college
            }
            if (!trainingLevel) {
                return this.$message({
                    type: 'error',
                    message: this.$t('course.selectTrainingLevel')
                })
            }
            if (!college) {
                return this.$message({
                    type: 'error',
                    message: this.$t('course.selectCollege')
                })
            }
            const temp = {
                trainingLevel: trainingLevel,
                college: college,
                managerDeptId: this.managerDeptId
            }
            // this.autoCodeInfo.trainingLevel = trainingLevel;
            // this.autoCodeInfo.managerDeptId = this.queryForm.managerDeptId;
            // this.autoCodeInfo.college = college;
            autoCode(temp).then(res => {
                if (res.code == 200) {
                    if (this.managerDeptId != 1) {
                        this.courseForm.code = res.data
                        this.$refs.courseFormCode.focus()
                    }
                    /* 	if () {
						this.underGraduateCourseForm.code = res.data;
						this.$refs.courseFormCode2.focus();
					} else {
						this.courseForm.code = res.data;
						this.$refs.courseFormCode.focus();
					} */
                }
            })
        },
        resetFormValidate() {
            // 清除表单校验
            this.$refs.courseForm.clearValidate()
            //this.courseForm = JSON.parse(JSON.stringify(defaultCourseForm));
        },
        // changeDialogCollege(val) {
        // 	findListByFaculty({ faculty: val }).then(res => {
        // 		this.options.teacherList = res.data;
        // 	});
        // },
        back() {
            this.resetFormValidate()
            this.$emit('back', 'main')
        },
        handleEnabled(val){
            if(this.pathKey=='edit'){
                this.$nextTick(()=>{
                    this.resetFormValidate();
                })
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.arrange-panel-header {
    height: 56px;
    border-bottom: 2px solid #dcdfe6;
    cursor: default;
    .title {
        font-size: 20px;
        color: #000;
        font-weight: bold;
    }
    .class-profiles {
        float: left;
        width: 40%;
        div {
            padding: 5px 0;
        }
    }
}
#course-form {
    ::v-deep .el-tag--mini {
        padding-left: 0;
    }
    ::v-deep .el-tooltip {
        margin: 0;
    }
    ::v-deep .el-select .is-disabled {
        height: 32px;
    }
}
#for-major ::v-deep .el-select__tags > span {
    & > span:first-child {
        width: calc(100% - 65px);
        display: inline-block;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}
#auto-btn {
    padding: 8px 15px;
}
</style>
